package com.example.yls.demoa;

import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btn_insert;
    private Button btn_query;
    private Button sco_insert;
    private Button sco_query;
    private Button sco_del;
    private Button sco_update;
    private Button stu_delect;
    private Button stu_update;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 实现数据插入
         */
        btn_insert = findViewById(R.id.stu_create);
        btn_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/student");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno","1001");
                contentValues.put("name","占弥散");
                contentValues.put("sex",1);
                contentValues.put("age",25);
                getContentResolver().insert(uri,contentValues);
            }
        });

        /**
         * 实现数据的查询
         */
        btn_query = findViewById(R.id.stu_query);
        result = findViewById(R.id.result);
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/student");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);

                StringBuffer buffer = new StringBuffer();
                if (cursor != null && cursor.getCount() > 0) {
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int sex = cursor.getInt(2);
                        int age = cursor.getInt(3);

                        buffer.append(stuno).append("  ").append(name).append("  ").append(sex).append(" ").append(age).append("\n");
                    }
                    result.setText(buffer);
                }else {
                    result.setText("无数据");
                }
            }
        });

        /**
         * 实现数据的删除
         */
        stu_delect = findViewById(R.id.stu_delect);
        stu_delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/student");
                String where = "name = 'Jame'";
                int i = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了" + i + "行数据",Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * 实现数据的更新
         */
        stu_update = findViewById(R.id.stu_update);
        stu_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/student");
                ContentValues value = new ContentValues();
                value.put("age",30);
                String where = "age = 25";
                int i = getContentResolver().update(uri,value,where,null);
                Toast.makeText(MainActivity.this,"更新了" + i + "行数据",Toast.LENGTH_SHORT).show();
            }
        });


        sco_insert = findViewById(R.id.sco_create);
        sco_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/score");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno","1001");
                contentValues.put("english",80);
                contentValues.put("Android",85);
                getContentResolver().insert(uri,contentValues);
            }
        });


        sco_query = findViewById(R.id.sco_query);
        sco_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/score");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);

                StringBuffer buffer = new StringBuffer();
                if (cursor != null && cursor.getCount() > 0) {
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        int english = cursor.getInt(1);
                        int Android = cursor.getInt(2);

                        buffer.append(stuno).append("  ").append(english).append("  ").append(Android).append("\n");
                    }
                    result.setText(buffer);
                }else {
                    result.setText("无数据");
                }
            }
        });

        sco_del = findViewById(R.id.sco_delect);

        sco_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/score");
                String where = "stuno = '1001'";
                int p = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了" + p + "行数据",Toast.LENGTH_SHORT).show();
            }
        });


        sco_update = findViewById(R.id.sco_update);
        sco_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://lee.student/score");
                ContentValues value = new ContentValues();
                value.put("english",100);
                String where = "stuno = '1001'";
                int i = getContentResolver().update(uri,value,where,null);
                Toast.makeText(MainActivity.this,"更新了" + i + "行数据",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
